home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
9-Digit Zip Code Directory
/
9-Digit Zip Code Directory (American Business Information) (ABIZIP-12).ISO
/
z4src.zip
/
Z4EXTRCT.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
1993-08-23
|
7KB
|
243 lines
//----------------------------------------------------------------------------
// MODULE DESCRIPTION
//
// Module: z4extrct.cpp
// Title: 9-Digit ZIP Code Directory -- on CD-ROM
// Notice: John M. Weeder
// Copyright (c) 1993. All rights reserved.
// This module contains proprietary information and should be
// treated as confidential.
//
//----------------------------------------------------------------------------
// MAINTENANCE HISTORY
//
// $Workfile$
// $Revision$
// $Author$
// $Date$
// $Log$
//
//----------------------------------------------------------------------------
// MODULE NARRATIVE
//
// This module contains the program entry point for
//
// The code in this module may be written in C++ or C.
//
// This module is portable to:
// DOS 3.X+
// MS Windows 3.X+
// OS/2 2.X+
// OS/2 2.0 PM
//
// The following compilers are supported:
// MSC 6.0A
// MSC/C++ 7.0
// Borland C++ 3.1 for DOS
// Borland C++ 1.0 for OS/2 2.X
//
//----------------------------------------------------------------------------
#include <z4.h>
//----------------------------------------------------------------------------
// Stack size
//----------------------------------------------------------------------------
#if COMPILER_BORLAND && (OS_DOS || OS_WINDOWS)
unsigned _stklen = 0x4000;
#endif
//----------------------------------------------------------------------------
// Globals
//----------------------------------------------------------------------------
//
// A useless string which is simply embedded in the executable.
//
PSZ __pszCredits__ = "Written by John M Weeder, 1993";
//
// This should contain a program description which will be displayed as
// part of the program's help text.
//
static PCSZ pcszDescription =
"This program extracts records from a ZIP+4 data file.";
//
// Program command line options
//
static Z4_Z4 z4;
static CHAR szOutput[MAX_PATH];
static HF hfOutput = -1;
static LONG lStart = 0;
static LONG lEnd = -1;
static LONG lLength = -1;
static LONG lRange = 1;
static LONG lSkip = 0;
static BOOL fAppend = FALSE;
static CHAR szFormat[1024];
static BS_CMDOPT acmdopt[] =
{
{ "APPEND", (PVOID)&fAppend, CMDOPT_TRUE, "Append records to output?"},
{ "BEGIN", (PVOID)&lStart, CMDOPT_NUMERIC, "First record to extract."},
{ "END", (PVOID)&lEnd, CMDOPT_NUMERIC, "Last record to extract."},
{ "LENGTH", (PVOID)&lLength, CMDOPT_NUMERIC, "Length of records to extract."},
{ "RANGE", (PVOID)&lRange, CMDOPT_NUMERIC, "Range of records to extract."},
{ "SKIP", (PVOID)&lSkip, CMDOPT_NUMERIC, "Number of records to skip."},
{ "output", (PVOID)szOutput, CMDOPT_FILESPECR( 80), "Output file name."},
BS_CMDOPT_NULL,
};
//----------------------------------------------------------------------------
// Description: main() - Program entry point
// Parameters: Standard C parameters
// Returns: DOS return code.
//----------------------------------------------------------------------------
BOOL FN_E Write()
{
PCSZ pszRecType = Z4Z4RecordType(z4.rectype, Z4_DATA_FULL);
if (z4.suffix2)
strcats(z4.szPriName, Z4SuffixFull(z4.suffix2));
sprintf(szFormat,
"%-30.30s %-4s %-6s - %6s %*s** %s **\r\n"
"%-10s - %-10s %-2s %-30s %-4s %-2s\r\n"
"%s %-4s - %-4s\r\n\r\n",
z4.szSecName,
Z4Unit(z4.unit, Z4_DATA_ABBREV),
z4.szSecLo,
z4.szSecHi,
20 - strlen(pszRecType), "",
pszRecType,
z4.szPriLo,
z4.szPriHi,
Z4Directional(z4.predir, Z4_DATA_ABBREV),
z4.szPriName,
Z4SuffixAbbrev(z4.suffix1),
Z4Directional(z4.postdir, Z4_DATA_ABBREV),
z4.szZip5,
z4.szAddonHi,
z4.szAddonLo);
return FileWrite(hfOutput, szFormat, strlen(szFormat), -1);
}
//----------------------------------------------------------------------------
// Description: main() - Program entry point
// Parameters: Standard C parameters
// Returns: DOS return code.
//----------------------------------------------------------------------------
int main(int argc, char **argv)
{
static BS_CFG cfg = CFG_DFT;
FLAG16 fsOutput = FL_READWRITE|FL_DENYREADWRITE|FL_BINARY;
PCSZ pcszData = EnvGet("DATA");
SHORT sResult = 99;
LONG lRecords;
BYTE bKey[2][MAX_Z4_KEY];
SIZET cWhich = 0;
LONG lLast = -1;
//
// Initialize base library
//
BaseLibraryInitialize(argc, argv, &cfg);
BaseTitle("$Revision: 93.1 $", __DATE__, __TIME__, "ZIP+4 Record Extract Utility");
if (!BaseTitleHelp(acmdopt, pcszDescription))
return 99;
if (szOutput[0]) // Open output file
{
if (!FnameQualify(szOutput, NULL, pcszData, 0))
goto ERROR_EXIT;
if (!fAppend)
fsOutput |= (FL_CREATE|FL_TRUNCATE);
else
fsOutput |= (FL_CREATE|FL_OPEN);
if (!FileOpen(&hfOutput, szOutput, fsOutput, NULL))
goto ERROR_EXIT;
Output("Opened output file '%s'\n", szOutput);
}
// Open data files
if (!Z4_INQ::Start(Z4_INQ_CS|Z4_INQ_CX|Z4_INQ_AB|Z4_INQ_ST|Z4_INQ_Z4|Z4_INQ_Z5))
{
Error(
"There is a problem accessing the data files.\n"
"Please verify that the program is configured correctly.");
goto ERROR_EXIT;
}
if (lStart < 0)
lStart = 0;
if (lSkip < 0)
lSkip = 0;
lRecords = Z4_INQ::z4_file.Records();
if (lRecords == 0)
{
Error("No records found in input file.\n");
goto ERROR_EXIT;
}
if (lStart >= lRecords)
{
Output("No records copied.\n");
goto ERROR_EXIT;
}
if (lLength > 0)
lEnd = lStart + lLength - 1;
if (lEnd >= lRecords || lEnd < lStart)
lEnd = lRecords - 1;
if (lRange <= 0)
lRange = 1;
memset(bKey, 0, sizeof(bKey));
while (lStart <= lEnd)
{
for (LONG l = 0; l < lRange && lStart <= lEnd; ++l, ++lStart)
{
if (lLast < 0 ||
((lStart - lLast) % 1000L) == 0)
{
while (KbdReady())
if (KbdChar() == ESC)
{
Output("\r%-40s\rAborted.\n", "");
goto ERROR_EXIT;
}
Output("\r%08ld", lStart);
lLast = lStart;
}
if (!Z4_INQ::z4_file.Record(z4, lStart))
{
Output("\n\nFailed reading record %ld.\n", lStart);
goto ERROR_EXIT;
}
cWhich = !cWhich;
if (szOutput[0]) // Open output file
if (!Write())
goto ERROR_EXIT;
Z4Z4Key(bKey[cWhich], z4.szFinance, z4.szPriName, z4.szPriLo);
if (memcmp(bKey[cWhich], bKey[!cWhich], MAX_Z4_KEY) < 0)
{
Output("\n\nInvalid sort sequence at record %ld.\n", lStart);
goto ERROR_EXIT;
}
}
lStart += lSkip; // Move to next record
}
Output("\r%08ld\nSuccess\n", lEnd);
ERROR_EXIT:
Z4_INQ::Terminate(); // Close data files
if (hfOutput >= 0) // Close output
FileClose(hfOutput);
return sResult;
}
//----------------------------------------------------------------------------
//------------------------------- End of File --------------------------------
//----------------------------------------------------------------------------